﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace LinqToLfsWorld.Extensions
{
	/// <summary>
	/// Contains various methods which extend the standard String class.
	/// </summary>
	public static class StringExtensions
	{

		/// <summary>
		/// Helper method for writing a string to a file.
		/// </summary>
		/// <param name="data">The data to write</param>
		/// <param name="filename">The name of the file to write to</param>
		/// <param name="append">If true, the data will be appended to the target file if the target file already exists.</param>
		internal static void ToFile(this string data, string filename, bool append)
		{
			FileMode mode = append ? FileMode.Append : FileMode.Create;

			using (Stream fStream = new FileStream(filename, mode))
			{
				using (StreamWriter w = new StreamWriter(fStream))
				{
					w.Write(data);
				}
			}
		}

		/// <summary>
		/// Helper for returning IsNullOrEmpty from a target string
		/// </summary>
		/// <param name="str">The string</param>
		internal static Boolean IsNullOrEmpty(this string str)
		{
			return string.IsNullOrEmpty(str);
		}

		/// <summary>
		/// Removes ^1 style colour markers from a string. Used when retrieving host and racer names from LfsWorld.
		/// </summary>
		/// <param name="str">The string to strip colour tags from</param>
		public static string StripLfsColours(this string str)
		{
			return Regex.Replace(str, @"\^[0-9]", string.Empty);
		}

	}
}
